﻿using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Service;
using NB.MGJ.Service.Origins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public class OriginsTempletController : BaseController
    {
        private ProductService _productService = new ProductService();
        private DemoService _demoService = new DemoService();
        private OriginsNodeService _Node = new OriginsNodeService();

        private OriginsTempletService _originsTempletService;
        private BasicOriginModuleService _basicOriginModule;
        public OriginsTempletController()
        {
            _demoService = new DemoService();
            _originsTempletService = new OriginsTempletService();
            _basicOriginModule = new BasicOriginModuleService();
        }



        #region 视图

        public ActionResult TempletList()
        {
            return View();
        }

        public ActionResult AddOriginsTemplet_P(int templetId=0)
        {

            var templetModel = _originsTempletService.GetModelById(templetId);
            if (templetModel == null)
            {
                templetModel = new Origins_Templet()
                {
                    TempletId = 0,
                    Name="",
                    Code="",
                    ClassifyId=0,
                    Remark="",
                };
            }
            ViewBag.templetModel = templetModel;
            ViewBag.eid = LogOnUser.EnterpriseId;
            return View();
        }


        public ActionResult NodeConfigAdd_PV()
        {
            return PartialView();
        }


        #endregion



        #region 增
        public int SaveTemplet(Origins_Templet model)
        {

            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.ViewDel = 0;
            model.EnterpriseId = LogOnUser.EnterpriseId;
            int result = model.TempletId == 0 ? _originsTempletService.Insert(model) : _originsTempletService.Update(model);
            return result;
        }


        #endregion 


        #region  删

        public ActionResult OriginsTempletDel(int Tid)
        {
            var result = _originsTempletService.Del(Tid);
            return Json(result);
        }

        #endregion


        #region 改



        #endregion 


        #region  查

        public ActionResult GetTempletListBySearch(int pageIndex, int pageSize, string searchKey = null, int showSys = 0, int classifyId = 0)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            //我的模板  
            if (showSys == 0)
            {
                pm.StrCondition = " Origins_Templet.EnterpriseId=" + LogOnUser.EnterpriseId;
            }
            //系统模板
            else if (showSys == 1)
            {
                pm.StrCondition = " Origins_Templet.EnterpriseId=1 ";
            }
            //我的模板和系统模板
            else if (showSys == 2)
            {
                pm.StrCondition = " (Origins_Templet.EnterpriseId=" + LogOnUser.EnterpriseId + " or Origins_Templet.EnterpriseId=1) ";
            }
            if (classifyId != 0 && classifyId != 1)
            {
                pm.StrCondition += " and ClassifyId=" + classifyId;
            }

            if (string.IsNullOrEmpty(pm.FldSort))
                pm.FldSort = " ChangeDate asc ";
            PagedList<Origins_Templet> list = _originsTempletService.GetTemplateList(ref pm);
            return Json(list);

        }

        public JsonResult PageList(string where, int pageIndex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            string str = " ViewDel=0 and EnterpriseId=" + LogOnUser.EnterpriseId;
            pm.StrCondition = where != null ? str + "  and  name like '%" + where + "%' " : str;
            var model = _originsTempletService.GetPageList(ref pm);
            return Json(model);

        }

        #endregion 


    }
}